//
//  PlayGhostLayer.cpp
//  NewMath
//
//  Created by Ngô Hoàng Nam on 1/3/15.
//
//

#include "PlayGhostLayer.h"
#include "../PlayManager.h"
#include "../../../BaseObject/BaseGhostObj.h"
#include "../Object/PlaySelectGhostObj.h"
#include "../../Level/LevelManager.h"
#include "../../../Data/GameData.h"
#include "../Object/PlayAdjustGhostObj.h"
PlayGhostLayer::PlayGhostLayer(Size screenSize)
{
	m_screenSize = screenSize;
}

void PlayGhostLayer::initalize(Node* nodeRoot)
{
	#pragma endregion
    int numRow = (int)nodeRoot->getChildrenCount();
    float canvasHeight = m_screenSize.height / (numRow + 1);
    float expectHeight = canvasHeight*0.6f;
    //int numGhost = cocos2d::random(0,(int)nodeRoot->getChildByName("nodeNormal")->getChildrenCount());
    
    int ghostCount = 0;
    for (int i = 0; i < numRow; i++)
    {
        Node* row = nodeRoot->getChildByName(cocos2d::String::createWithFormat("%s%i", "Row_", i + 1)->getCString());
        int numCol = (int)row->getChildrenCount();
        for (int j = 0; j < numCol; j++)
        {
            ghostCount++;
        }
    }
    
    for (int i = 0,indexGhost = 1; i < numRow; i++)
    {
        Node* row = nodeRoot->getChildByName(cocos2d::String::createWithFormat("%s%i", "Row_", i + 1)->getCString());
        int numCol = (int)row->getChildrenCount();
        for (int j = 0; j < numCol; j++,indexGhost++)
        {
            PlayAdjustGhostObj* ghost = new PlayAdjustGhostObj();
            
            switch (LevelManager::m_ELevelMode) {
                case Easy:
                   
                    ghost->initGhost("ghost_blue.png", cocos2d::String::createWithFormat("%d", random(0, 15))->getCString(), Vec2::ZERO, 1);
                   
                    break;
                case Normal:
                    ghost->initGhost("ghost_red.png", cocos2d::String::createWithFormat("%d", random(0, 15))->getCString(), Vec2::ZERO, 1);
                    break;
                case Hard:
                    ghost->initGhost("ghost_purple.png", cocos2d::String::createWithFormat("%d", random(0, 15))->getCString(), Vec2::ZERO, 1);
                    break;
                default:
                    break;
                    
            }
            Node* nodeGhost= row->getChildByName(cocos2d::String::createWithFormat("%s%d", "ghost_", indexGhost)->getCString());
            BaseGhostObj::addToLayer(nodeGhost, ghost);
        }
        
    }
    nodeRoot->setVisible(true);
}

void PlayGhostLayer::initalize(Node* nodeRoot, int correctValue)
{
	int numRow = (int)nodeRoot->getChildrenCount();
	float canvasHeight = m_screenSize.height / (numRow + 1);
	float expectHeight = canvasHeight*0.6f;
	//int numGhost = cocos2d::random(0,(int)nodeRoot->getChildByName("nodeNormal")->getChildrenCount());

	int ghostCount = 0;
	for (int i = 0; i < numRow; i++)
	{
		Node* row = nodeRoot->getChildByName(cocos2d::String::createWithFormat("%s%i", "Row_", i + 1)->getCString());
		int numCol = (int)row->getChildrenCount();
		for (int j = 0; j < numCol; j++)
		{
			ghostCount++;
		}
	}

	int posAnswer = cocos2d::random(0, ghostCount - 1);

	for (int i = 0,indexGhost = 1; i < numRow; i++)
	{
		Node* row = nodeRoot->getChildByName(cocos2d::String::createWithFormat("%s%i", "Row_", i + 1)->getCString());
		int numCol = (int)row->getChildrenCount();
		for (int j = 0; j < numCol; j++,indexGhost++)
		{
			PlaySelectGhostObj* ghost = new PlaySelectGhostObj();

			switch (LevelManager::m_ELevelMode) {
			case Easy:

				if (j == posAnswer)
				{
					auto str = cocos2d::String::createWithFormat("%d",
						correctValue)->getCString();
					auto nodeName = cocos2d::String::createWithFormat("%s%d", "ghost_", indexGhost)->getCString();
					Vec2 pos = row->getChildByName(nodeName)->getPosition();
					ghost->initGhost("ghost_blue.png",str ,pos , 1);
				}
				else
				{
					ghost->initGhost("ghost_blue.png", cocos2d::String::createWithFormat("%d", random(0, 15))->getCString(), row->getChildByName(cocos2d::String::createWithFormat("%s%d", "ghost_", indexGhost)->getCString())->getPosition(), 1);
				}
				break;
			case Normal:
				if (j == posAnswer)
				{
					const char* str = String::createWithFormat("%d", GamePlayConfig::GetInstance()->m_hiddenNumber)->getCString();
					ghost->initGhost("ghost_red.png", cocos2d::String::createWithFormat("%d",
						correctValue)->getCString(), row->getChildByName(cocos2d::String::createWithFormat("%s%d", "ghost_", indexGhost)->getCString())->getPosition(), 1);

				}
				else
				{
					ghost->initGhost("ghost_red.png", cocos2d::String::createWithFormat("%d", random(0, 15))->getCString(), row->getChildByName(cocos2d::String::createWithFormat("%s%d", "ghost_", indexGhost)->getCString())->getPosition(), 1);
				}
				break;
			case Hard:
				if (j == posAnswer)
				{
					const char* str = String::createWithFormat("%d", GamePlayConfig::GetInstance()->m_hiddenNumber)->getCString();
					ghost->initGhost("ghost_purple.png", cocos2d::String::createWithFormat("%d",
						correctValue)->getCString(), row->getChildByName(cocos2d::String::createWithFormat("%s%d", "ghost_", indexGhost)->getCString())->getPosition(), 1);
				}
				else
				{
					ghost->initGhost("ghost_purple.png", cocos2d::String::createWithFormat("%d", random(0, 15))->getCString(), row->getChildByName(cocos2d::String::createWithFormat("%s%d", "ghost_", indexGhost)->getCString())->getPosition(), 1);
				}
				break;
			default:
				break;

			}
			BaseGhostObj::addToLayer(nodeRoot, ghost);
		}
		
	}
	nodeRoot->setVisible(true);
}